package com.help10000.rms.ui.activitys

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.amir.common.api.CustomObserver
import com.amir.common.beans.BaseResult
import com.amir.common.beans.CompanyData
import com.amir.common.beans.CompanyEntity
import com.amir.common.mvp.model.Model
import com.amir.common.utils.AppManager
import com.amir.common.utils.Constant
import com.amir.common.utils.SPUtils
import com.amir.common.weight.DuplicateClickProxy
import com.amir.common.weight.SpaceItemDecoration
import com.help10000.rms.R
import com.help10000.rms.config.globalData
import com.help10000.rms.mvp.presenter.SelectCompanyPresenter
import com.help10000.rms.mvp.view.SelectCompanyView
import com.help10000.rms.ui.activitys.logins.LoginActivity
import com.help10000.rms.ui.adapter.CompanyAdapter
import com.help10000.rms.ui.base.BaseActivity
import com.help10000.rms.ui.utils.PageUtils
import kotlinx.android.synthetic.main.activity_select_company.*

/**
 * 选择公司
 */
class SelectCompanyActivity : BaseActivity<SelectCompanyPresenter, SelectCompanyView>(),
    SelectCompanyView {
    //如果是在登录后个人中心选择公司1，直接关闭当前页面,正常登录，是0
    var flag: Int = 0
    override fun getlayoutId(): Int {
        return R.layout.activity_select_company
    }

    override fun initView() {
        initRefreshLayout(refresh)
// 关键逻辑：根据 flag 判断上一页是否为 LoginActivity，控制返回按钮可见性

    }

    override fun enableLoadMore(): Boolean {
        return false
    }

    override fun enableRefresh(): Boolean {
        return false
    }

    override fun initData(savedInstanceState: Bundle?) {
        val data = intent.getSerializableExtra("data") as CompanyData?
        flag = intent.getIntExtra("flag", 0)
        if (data != null) {
            val linearLayoutManager = LinearLayoutManager(mContext)
            rv_list.layoutManager = linearLayoutManager
            val companyAdapter = CompanyAdapter(mContext, data.CmpyList, R.layout.item_conpany)
            rv_list.adapter = companyAdapter
            rv_list.addItemDecoration(SpaceItemDecoration(0, 10))
            companyAdapter.setOnItemClickListener {
                mPresenter.companyLogin(data.CmpyList[it])
                SPUtils.save(Constant.CmpyName, data.CmpyList[it].CmpyName)
            }
        } else {
            mPresenter.companyList()
        }
        if (flag == 0) {
            // flag=0 对应上一页是 LoginActivity，隐藏返回按钮
            val ivBack = titleBar.getView<ImageView>(R.id.iv_back) // 通过 CustomToolBar 的 getView 方法获取返回按钮
            ivBack.visibility = View.GONE
        } else {
            // 其他场景（如个人中心切换公司），显示返回按钮
            val ivBack = titleBar.getView<ImageView>(R.id.iv_back)
            ivBack.visibility = View.VISIBLE
        }
    }


    override fun initListener() {
        titleBar.setOnLeftClickListener {
            if (flag == 0) {
                SPUtils.remove(Constant.CmpyCode)
                SPUtils.remove(Constant.pwd)
                AppManager.getAppManager().finishAllActivity()
                PageUtils.jumpToActivity(this, LoginActivity::class.java)
            }
            finish()
        }
        btn_exit.setOnClickListener(DuplicateClickProxy {

            if (flag == 0) {
                // 上一页是登录界面：仅返回上一页（不清除数据，保留登录页状态）
                SPUtils.remove(Constant.CmpyCode)
                SPUtils.remove(Constant.pwd)
                SPUtils.remove(Constant.accountName)
                SPUtils.remove(Constant.encodePassword)
                SPUtils.remove(Constant.nonceStr)
                SPUtils.remove(Constant.read_privacy)
                finish()
            } else {
                // 上一页是其他页面：执行完整退出登录逻辑（清除数据并跳转登录页）
                SPUtils.remove(Constant.CmpyCode)
                SPUtils.remove(Constant.pwd)
                SPUtils.remove(Constant.accountName)
                SPUtils.remove(Constant.encodePassword)
                SPUtils.remove(Constant.nonceStr)
                SPUtils.remove(Constant.read_privacy)
                PageUtils.jumpToActivity(this, LoginActivity::class.java)
                // 关闭当前页面和其他所有页面，确保回到登录页
                AppManager.getAppManager().finishAllActivity()
            }
        })
    }

    override fun companyLoginSuccess() {
        if (flag == 0) {
            AppManager.getAppManager().finishActivity(LoginActivity::class.java)
            var intent = Intent(mContext, MainActivity::class.java)
            startActivity(intent)
        }
        else if (flag == 1) {
//            val intent = Intent("com.action.menu.reload")
//            sendBroadcast(intent)
            AppManager.getAppManager().finishAllActivity()
            var intent = Intent(mContext, MainActivity::class.java)
            startActivity(intent)
        }
        finish()
    }

    override fun companyListData(t: CompanyEntity) {
        val linearLayoutManager = LinearLayoutManager(mContext)
        rv_list.layoutManager = linearLayoutManager
        val companyAdapter = CompanyAdapter(mContext, t.data.CmpyList, R.layout.item_conpany)
        rv_list.adapter = companyAdapter
        rv_list.addItemDecoration(SpaceItemDecoration(0, 10))
        companyAdapter.setOnItemClickListener {
            mPresenter.companyLogin(t.data.CmpyList[it])

        }
    }

    override fun createPresenter(): SelectCompanyPresenter {
        return SelectCompanyPresenter(this)
    }
}